/*
 * *
 *  * Created with IntelliJ RustRover.
 *  * Description:
 *  * Copyright Author: ZhenYi
 *  * DateTime: 2024/2/15 下午9:09
 *
 */


mod login{
    use reqwest::header::{HeaderMap, HeaderValue};
    use crate::HOST;

    /// 登录测试
    /// status: 202 登陆成功 226 登陆失败
    /// cookie: 保存用户信息
    /// basic auth: 保存在indexedDB
    #[derive(serde::Serialize, serde::Deserialize)]
    struct User{
        username: String,
        password: String
    }
    #[tokio::test]
    pub async fn login_admin(){
        let input = User{
            username: "admin".to_string(),
            password: "admin".to_string()
        };
        let res = reqwest::Client::new()
            .post(format!("{}/login/admin", HOST))
            .json(&input)
            .send()
            .await
            .unwrap();

        println!("Url: {}", res.url());
        println!("Status: {}", res.status());
        for cookie in res.cookies() {
            println!("Cookie: {:?}", cookie);
        }
        println!("Headers: {:?}", res.headers());
        println!("Text: {:?}", res.text().await.unwrap());
    }
    #[tokio::test]
    pub async fn login_author(){
        let input = User{
            username: "admin".to_string(),
            password: "admin".to_string()
        };
        let res = reqwest::Client::new()
            .post(format!("{}/login/author", HOST))
            .json(&input)
            .send()
            .await
            .unwrap();
        println!("Url: {}", res.url());
        println!("Status: {}", res.status());
        for cookie in res.cookies() {
            println!("Cookie: {:?}", cookie);
        }
        println!("Headers: {:?}", res.headers());
        println!("Text: {:?}", res.text().await.unwrap());
    }
    #[tokio::test]
    pub async fn login_expert(){
        let input = User{
            username: "admin".to_string(),
            password: "admin".to_string()
        };
        let res = reqwest::Client::new()
            .post(format!("{}/login/expert", HOST))
            .json(&input)
            .send()
            .await
            .unwrap();
        println!("Url: {}", res.url());
        println!("Status: {}", res.status());
        for cookie in res.cookies() {
            println!("Cookie: {:?}", cookie);
        }
        println!("Headers: {:?}", res.headers());
        println!("Text: {:?}", res.text().await.unwrap());
    }
    #[tokio::test]
    pub async fn local(){
        let mut head = HeaderMap::new();
        head.insert("Cookie",HeaderValue::from_str("Cookie:F88162C9-0C96-AF37-DA5B-A9BD14CAD670").unwrap());
        let res = reqwest::Client::new()
            .post(format!("{}/local", HOST))
            .headers(head)
            .send()
            .await
            .unwrap();
        println!("Url: {}", res.url());
        println!("Status: {}", res.status());
        for cookie in res.cookies() {
            println!("Cookie: {:?}", cookie);
        }
        println!("Headers: {:?}", res.headers());
        println!("Text: {:?}", res.text().await.unwrap());
    }
}